/******************************************************************************
 * 
 * Announce: CSharpKit, Basic algorithms, components and definitions.
 *           Copyright (C) ShenYongchen.
 *           All rights reserved.
 *   Author: 申永辰.郑州 (shenyczz@163.com)
 *  WebSite: http://gitee.com/shenyczz/csharp_csk
 *
 * THIS CODE IS LICENSED UNDER THE MIT LICENSE (MIT).
 * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF 
 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
 * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
 * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
 * 
******************************************************************************/

using System;
using System.Runtime.InteropServices;
using CSharpKit.Convertions;

namespace CSharpKit.Native.ECCODES
{
    // 
    // codes_handle The message handle
    // The codes_handle is the structure giving access to parsed message values by keys.
    // 
    partial class eccodes
    {
        /// <summary>
        /// 原型: codes_handle* codes_handle_new_from_file(codes_context* c, FILE* f, ProductKind product, int* error) <br/>
        /// 功能: Create a handle from a file resource. <br/>
        ///  The file is read until a message is found. The message is then copied.
        ///  Remember always to delete the handle when it is not needed anymore to avoid
        ///  memory leaks. <br/>
        /// 状态: ok <br/>
        /// </summary>
        /// <param name="context">the context from which the handle will be created (NULL for default context)</param>
        /// <param name="hfile">the file resource</param>
        /// <param name="kind">the kind of product e.g. PRODUCT_GRIB, PRODUCT_BUFR</param>
        /// <param name="err">error code set if the returned handle is NULL and the end of file is not reached</param>
        /// <returns> codes_handle* the new handle, NULL if the resource is invalid or a problem is encountered. </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_handle_new_from_file")]
        public extern static IntPtr codes_handle_new_from_file(IntPtr context, IntPtr hfile, ProductKind kind, out int err);

        /// <summary>
        /// 原型: int codes_get_string(const codes_handle* h, const char* key, char* mesg, size_t* length) <br/>
        /// 功能:  <br/>
        /// 状态:  ok <br/>
        /// </summary>
        /// <param name="handle">codes_handle</param>
        /// <param name="key"></param>
        /// <param name="msg">byte[]</param>
        /// <param name="length"></param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Ansi, EntryPoint = "codes_get_string")]
        internal extern static int _codes_get_string(IntPtr handle, string key, byte[] msg, ref int length);
        public static bool codes_get_string(IntPtr handle, string key, out string out_string)
        {
            out_string = "";

            int len = BUFFER_SIZE;
            byte[] buffer = new byte[len];
            int ret = _codes_get_string(handle, key, buffer, ref len);
            if (len > BUFFER_SIZE)
            {
                buffer[BUFFER_SIZE - 1] = 0;
            }

            // out_string = BytesConverter.Instance.ToString(buffer);
            out_string = KConverter.BytesConverter.ToString(buffer);

            return ret == 0;
        }


        /// <summary>
        /// 原型: int codes_get_size(const codes_handle* h, const char* key, size_t* size) <br/>
        /// 功能: Get the number of coded value from a key, if several keys of the same name are present, the total sum is returned <br/>
        /// 状态:  <br/>
        /// </summary>
        /// <param name="handle"> the handle to get the offset from </param>
        /// <param name="key">the key to be searched</param>
        /// <param name="out_size">the address of a size_t where the size will be set</param>
        /// <returns>0 if OK, integer value on error</returns>
        /// <remarks>CharSet must be ansi</remarks>
        [DllImport(_dllname_, CharSet = CharSet.Ansi, EntryPoint = "codes_get_size")]
        public extern static int codes_get_size(IntPtr handle, string key, out uint out_size);

        /// <summary>
        /// 原型: int codes_get_double_array(const codes_handle* h, const char* key, double* vals, size_t* length) <br/>
        /// 功能: Get double array values from a key. If several keys of the same name are present, the last one is returned <br/>
        /// 状态:  <br/>
        /// </summary>
        /// <param name="handle">the handle to get the data from</param>
        /// <param name="key">the key to be searched</param>
        /// <param name="values">the address of a double array where the data will be retrieved</param>
        /// <param name="length">the address of a size_t that contains allocated length of the double array on input, and that contains the actual length of the double array on output</param>
        /// <returns></returns>
        /// <seealso cref="codes_set_double_array" />
        [DllImport(_dllname_, CharSet = CharSet.Ansi, EntryPoint = "codes_get_double_array")]
        public extern static int codes_get_double_array(IntPtr handle, string key, double[] values, ref uint length);


        /// <summary>
        /// 原型: int codes_count_in_file(codes_context* c, FILE* f, int* n); <br/>
        /// 功能: Counts the messages contained in a file resource. <br/>
        /// 状态:  <br/>
        /// </summary>
        /// <param name="codes_context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="hfile"> the file resource </param>
        /// <param name="n"> the number of messages in the file </param>
        /// <returns> 0 if OK, integer value on error </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_count_in_file")]
        public extern static int codes_count_in_file(IntPtr codes_context, IntPtr hfile, out int n);


        /// <summary>
        /// 原型: int codes_count_in_filename(codes_context* c, const char* filename, int* n); <br/>
        /// 功能: Counts the messages contained in a file. <br/>
        /// 状态:  <br/>
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="filename"> the path to the file </param>
        /// <param name="n"> the number of messages in the file </param>
        /// <returns> 0 if OK, integer value on error </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_count_in_filename")]
        public extern static IntPtr codes_count_in_filename(IntPtr context, string filename, out int n);


        /// <summary>
        /// 原型: codes_handle* codes_grib_handle_new_from_file(codes_context* c, FILE* f, int* error); <br/>
        ///  The file is read until a GRIB message is found. The message is then copied.
        ///  Remember always to delete the handle when it is not needed anymore to avoid
        ///  memory leaks.
        /// 功能: Create a GRIB handle from a file resource.  <br/>
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="hfile"> the file resource </param>
        /// <param name="err"> error code set if the returned handle is NULL and the end of file is not reached </param>
        /// <returns> the new handle, NULL if the resource is invalid or a problem is encountered </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_handle_new_from_file")]
        public extern static IntPtr codes_grib_handle_new_from_file(IntPtr context, IntPtr hfile, out int err);


        /**
        *  Create a BUFR handle from a file resource.
        *  The file is read until a BUFR message is found. The message is then copied.
        *  Remember always to delete the handle when it is not needed anymore to avoid
        *  memory leaks.
        *
        * @param c           : the context from which the handle will be created (NULL for default context)
        * @param f           : the file resource
        * @param error       : error code set if the returned handle is NULL and the end of file is not reached
        * @return            the new handle, NULL if the resource is invalid or a problem is encountered
        */
        /// <summary>
        /// 原型: codes_handle* codes_bufr_handle_new_from_file(codes_context* c, FILE* f, int* error); <br/>
        /// 功能: <br/>
        /// </summary>
        /// <param name="context"> </param>
        /// <param name="hfile"> </param>
        /// <param name="err"> </param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_bufr_handle_new_from_file")]
        public extern static IntPtr codes_bufr_handle_new_from_file(IntPtr context, IntPtr hfile, out int err);

        /// <summary>
        /// 原型: int codes_write_message(const codes_handle* h, const char* file, const char* mode); <br/>
        /// 功能: Write a coded message to a file.<br/>
        /// </summary>
        /// <param name="handle"> codes_handle to be written </param>
        /// <param name="filename"> name of the output file </param>
        /// <param name="mode"> </param>
        /// <returns> 0 if OK, integer value on error </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_write_message")]
        public extern static int codes_write_message(IntPtr handle, string filename, string mode);

        /// <summary>
        /// 原型: codes_handle* codes_grib_util_sections_copy(codes_handle* hfrom, codes_handle* hto, int what, int* err); <br/>
        /// 功能: <br/>
        /// </summary>
        /// <param name="hfrom"> </param>
        /// <param name="hto"> </param>
        /// <param name="what"> </param>
        /// <param name="err"> </param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_util_sections_copy")]
        public extern static IntPtr codes_grib_util_sections_copy(IntPtr hfrom, IntPtr hto, int what, out int err);


        /// <summary>
        /// 原型: codes_string_list* codes_grib_util_get_param_id(const char* mars_param); <br/>
        /// 功能: <br/>
        /// </summary>
        /// <param name="mars_param"> </param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_util_get_param_id")]
        public extern static IntPtr codes_grib_util_get_param_id(string mars_param);

        /// <summary>
        /// 原型: codes_string_list* codes_grib_util_get_mars_param(const char* param_id); <br/>
        /// 功能: <br/>
        /// </summary>
        /// <param name="param_id"> </param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_util_get_mars_param")]
        public extern static IntPtr codes_grib_util_get_mars_param(string param_id);


        /// <summary>
        /// 原型: codes_handle* codes_handle_new_from_message(codes_context* c, const void* data, size_t data_len); <br/>
        /// 功能: Create a handle from a user message in memory. <br/>
        ///     The message will not be freed at the end.
        ///     The message will be copied as soon as a modification is needed.
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="data"> the actual message </param>
        /// <param name="data_len"> the length of the message in number of bytes </param>
        /// <returns> the new handle, NULL if the message is invalid or a problem is encountered </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_handle_new_from_message")]
        public extern static IntPtr codes_handle_new_from_message(IntPtr context, IntPtr data, int data_len);


        /// <summary>
        /// 原型: codes_handle* codes_grib_handle_new_from_multi_message(codes_context* c, void** data, size_t* data_len, int* error); <br/>
        /// 功能: Create a handle from a user message in memory. <br/>
        ///     The message will not be freed at the end.
        ///     The message will be copied as soon as a modification is needed.
        ///     This function works also with multi-field messages.
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="data"> the actual message </param>
        /// <param name="data_len"> the length of the message in number of bytes </param>
        /// <param name="error"> error code </param>
        /// <returns> the new handle, NULL if the message is invalid or a problem is encountered </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_handle_new_from_multi_message")]
        public extern static IntPtr codes_grib_handle_new_from_multi_message(IntPtr context, IntPtr data, int data_len, out int error);


        /// <summary>
        /// 原型: codes_handle* codes_handle_new_from_message_copy(codes_context* c, const void* data, size_t data_len); <br/>
        /// 功能: Create a handle from a user message. The message is copied and will be freed with the handle. <br/>
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="data"> the actual message </param>
        /// <param name="data_len"> the length of the message in number of bytes </param>
        /// <returns> the new handle, NULL if the message is invalid or a problem is encountered </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_handle_new_from_message_copy")]
        public extern static IntPtr codes_handle_new_from_message_copy(IntPtr context, IntPtr data, int data_len);


        /// <summary>
        /// 原型: codes_handle* codes_grib_handle_new_from_samples(codes_context* c, const char* sample_name); <br/>
        /// 功能: Create a handle from a GRIB message contained in the samples directory. The message is copied at the creation of the handle. <br/>
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="sample_name"> the name of the sample file (without the .tmpl extension) </param>
        /// <returns> the new handle, NULL if the resource is invalid or a problem is encountered. </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_handle_new_from_samples")]
        public extern static IntPtr codes_grib_handle_new_from_samples(IntPtr context, string sample_name);



        /// <summary>
        /// 原型: codes_handle* codes_bufr_handle_new_from_samples(codes_context* c, const char* sample_name); <br/>
        /// 功能: Create a handle from a BUFR message contained in a samples directory. The message is copied at the creation of the handle. <br/>
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <param name="sample_name"> the name of the sample file (without the .tmpl extension) </param>
        /// <returns> the new handle, NULL if the resource is invalid or a problem is encountered </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_bufr_handle_new_from_samples")]
        public extern static IntPtr codes_bufr_handle_new_from_samples(IntPtr context, string sample_name);


        /// <summary>
        /// 原型: codes_handle* codes_handle_clone(const codes_handle* h); <br/>
        /// 功能: Clone an existing handle using the context of the original handle, The message is copied and reparsed. <br/>
        /// </summary>
        /// <param name="handle"> The handle to be cloned </param>
        /// <returns> the new handle, NULL if the message is invalid or a problem is encountered </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_handle_clone")]
        public extern static IntPtr codes_handle_clone(IntPtr handle);


        /// <summary>
        /// 原型: int codes_handle_delete(codes_handle* h) <br/>
        /// 功能: Frees a handle, also frees the message if it is not a user message <br/>
        /// </summary>
        /// <param name="handle"> The handle to be deleted </param>
        /// <returns> 0 if OK, integer value on error </returns>
        [DllImport(_dllname_, CharSet = CharSet.Ansi, EntryPoint = "codes_handle_delete")]
        public extern static int codes_handle_delete(IntPtr handle);


        /// <summary>
        /// 原型: codes_multi_handle* codes_grib_multi_handle_new(codes_context* c); <br/>
        /// 功能: Create an empty multi-field GRIB handle. <br/>
        ///  This is only applicable to GRIB edition 2.
        ///  Remember always to delete the multi-handle when it is not needed any more to avoid
        ///  memory leaks.
        /// </summary>
        /// <param name="context"> the context from which the handle will be created (NULL for default context) </param>
        /// <returns> </returns>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_multi_handle_new")]
        public extern static IntPtr codes_grib_multi_handle_new(IntPtr context);


        /// <summary>
        /// <para>功能: Append the sections starting with start_section of the message pointed by h at the end of the multi-field GRIB handle mh. </para>
        /// <para>说明: This is only applicable to GRIB edition 2. Remember always to delete the multi-handle when it is not needed any more to avoid memory leaks.</para>
        /// </summary>
        /// <param name="handle"> The handle from which the sections are copied. </param>
        /// <param name="start_section"> Section number. Starting from this section all the sections to the end of the message will be copied. </param>
        /// <param name="mh"> The multi-field handle on which the sections are appended. </param>
        /// <returns> 0 if OK, integer value on error </returns>
        /// <remarks>
        /// 原型: int codes_grib_multi_handle_append(codes_handle* h, int start_section, codes_multi_handle* mh); <br/>
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_multi_handle_append")]
        public extern static int codes_grib_multi_handle_append(IntPtr handle, int start_section, IntPtr mh);


        /// <summary>
        /// <para>功能: Delete multi-field GRIB handle. </para>
        /// <para>说明: This is only applicable to GRIB edition 2. </para>
        /// </summary>
        /// <param name="mh"> The multi-field handle to be deleted. </param>
        /// <returns> 0 if OK, integer value on error </returns>
        /// <remarks>
        /// 原型: int codes_grib_multi_handle_delete(codes_multi_handle* mh) <br/>
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_multi_handle_delete")]
        public extern static int codes_grib_multi_handle_delete(IntPtr mh);


        /// <summary>
        /// <para>功能: Write a multi-field GRIB handle in a file. </para>
        /// <para>说明: This is only applicable to GRIB edition 2. Remember always to delete the multi-handle when it is not needed any more to avoid memory leaks.</para>
        /// </summary>
        /// <param name="mh"> The multi-field GRIB handle to be written. </param>
        /// <param name="hfile"> File on which the file handle is written. </param>
        /// <returns> 0 if OK, integer value on error </returns>
        /// <remarks>
        /// 原型: int codes_grib_multi_handle_write(codes_multi_handle* mh, FILE* f) <br/>
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_grib_multi_handle_write")]
        public extern static int codes_grib_multi_handle_write(IntPtr mh, IntPtr hfile);



        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="handle"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: void codes_update_sections_lengths(codes_handle* h)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_update_sections_lengths")]
        public static extern void codes_update_sections_lengths(IntPtr handle);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="hin"> </param>
        /// <param name="hout"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_bufr_copy_data(codes_handle* hin, codes_handle* hout)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_bufr_copy_data")]
        public static extern int codes_bufr_copy_data(IntPtr hin, IntPtr hout);


        /// <summary>
        /// <para>功能: 
        /// codes_bufr_copy_data copies all the values in the data section that are present in the same position in the data tree
        /// and with the same number of values to the output handle. Should not exit with error if the output handle has a different
        /// structure as the aim is to copy what is possible to be copied.
        /// This will allow the user to add something to a message by creating a new message with additions or changes to the
        /// unexpandedDescriptors and copying what is possible to copy from the original message. 
        /// </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="hin"> </param>
        /// <param name="hout"> </param>
        /// <param name="nkeys"> </param>
        /// <param name="err"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: char** codes_bufr_copy_data_return_copied_keys(codes_handle* hin, codes_handle* hout, size_t* nkeys, int* err);
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_bufr_copy_data_return_copied_keys")]
        public static extern IntPtr codes_bufr_copy_data_return_copied_keys(IntPtr hin, IntPtr hout, int nkeys, int err);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="handle"> </param>
        /// <param name="name"> </param>
        /// <param name="type"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_get_native_type(const codes_handle* h, const char* name, int* type)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_get_native_type")]
        public static extern int codes_get_native_type(IntPtr handle, string name, out int type);



        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="handle"> </param>
        /// <param name="key"> </param>
        /// <param name="err"> </param>
        /// <returns> Returns a bool i.e. 0 or 1. The error code is the final argument </returns>
        /// <remarks>
        /// 原型: int codes_is_missing(const codes_handle* h, const char* key, int* err);
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_is_missing")]
        public static extern int codes_is_missing(IntPtr handle, string key, out int err);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="handle"> </param>
        /// <param name="key"> </param>
        /// <returns> a bool i.e. 0 or 1 </returns>
        /// <remarks>
        /// 原型: int codes_is_defined(const codes_handle* h, const char* key)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_is_defined")]
        public static extern int codes_is_defined(IntPtr handle, string key);


        /// <summary>
        /// <para>功能: Returns 1 if the BUFR key is in the header</para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="handle"> </param>
        /// <param name="key"> </param>
        /// <param name="err">The error code is the final argument </param>
        /// <returns> 0 if it is in the data section. </returns>
        /// <remarks>
        /// 原型: int codes_bufr_key_is_header(const codes_handle* h, const char* key, int* err)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_bufr_key_is_header")]
        public static extern int codes_bufr_key_is_header(IntPtr handle, string key, out int err);

        /// <summary>
        /// <para>功能: </para>
        /// <para>说明: </para>
        /// </summary>
        /// <param name="handle"> </param>
        /// <param name="key"> </param>
        /// <returns> </returns>
        /// <remarks>
        /// 原型: int codes_set_missing(codes_handle* h, const char* key)
        /// </remarks>
        [DllImport(_dllname_, CharSet = CharSet.Auto, EntryPoint = "codes_set_missing")]
        public static extern int codes_set_missing(IntPtr handle, string key);
    




        // {{END}}
    }

}

